package com.jy.appmarket.receiver;

import java.lang.reflect.Method;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;

import com.jy.appmarket.R;
import com.jy.appmarket.activity.DownloadManagerActivity;
import com.jy.appmarket.download.DownloadItem;
import com.jy.appmarket.download.DownloadService;
import com.jy.appmarket.model.AppExtras;
import com.jy.appmarket.utils.NotificationUtil;

public class AppActionReceiver extends BroadcastReceiver {

	public static final String ACTION_OPEN = "com.jy.appmarket.action.OPEN";
	public static final String ACTION_INSTALL = "com.jy.appmarket.action.INSTALL";
	public static final String ACTION_DOWNLOAD = "com.jy.appmarket.action.DOWNLOAD";
	public static final String APP_EXTRAS = "app_extras";
	public static final int NOTIFICATION_ID_APP_ADD_TO_DOWNLOAD_LIST = 3344;

	private Handler mHandler = new Handler();

	@Override
	public void onReceive(final Context context, Intent intent) {

		collapseStatusBar(context);
		NotificationUtil.cancelNotification(context, PushReceiver.NOTIFICATION_APP_ID);
		final AppExtras extras = (AppExtras) intent.getSerializableExtra(APP_EXTRAS);
		if (extras == null) {
			return;
		}
		if (ACTION_OPEN.equalsIgnoreCase(intent.getAction())) {
			openApp(context, extras.getPkgName());
			PushReceiver.sendConsumeBroadcast(context, extras.getPushId(), 0);
		} else if (ACTION_INSTALL.equalsIgnoreCase(intent.getAction())) {
			Intent i = new Intent(context, DownloadService.class);
			i.putExtra(DownloadService.EXTRA_DOWNLOADMODEL, getDownloadItem(extras));
			i.setAction(DownloadService.ACTION_START_INSTALL);
			context.startService(i);
			PushReceiver.sendConsumeBroadcast(context, extras.getPushId(), 3);
		} else if (ACTION_DOWNLOAD.equalsIgnoreCase(intent.getAction())) {
			Intent i = new Intent(context, DownloadService.class);
			i.putExtra(DownloadService.EXTRA_DOWNLOADMODEL, getDownloadItem(extras));
			i.putExtra(DownloadService.EXTRA_OPEN_DOWNLOADMGR, false);
			i.setAction(DownloadService.ACTION_START_DOWNLOAD);
			context.startService(i);

			PushReceiver.sendConsumeBroadcast(context, extras.getPushId(), 2);
			// show notification that app is add to download list;
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {

					NotificationUtil.showSingleLineNotification(context, NOTIFICATION_ID_APP_ADD_TO_DOWNLOAD_LIST,
							String.format(context.getString(R.string.add_app_to_download_list), extras.getAppName()),
							DownloadManagerActivity.class);
				}
			}, 500);
		}

	}

	private void openApp(Context context, String pkgName) {
		try {
			Intent intent = context.getPackageManager().getLaunchIntentForPackage(pkgName);
			context.startActivity(intent);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Collapse status panel
	 * 
	 * @param context
	 *            the context used to fetch status bar manager
	 */
	public void collapseStatusBar(Context context) {
		try {
			Object statusBarManager = context.getSystemService("statusbar");
			Method collapse;

			if (Build.VERSION.SDK_INT <= 16) {
				collapse = statusBarManager.getClass().getMethod("collapse");
			} else {
				collapse = statusBarManager.getClass().getMethod("collapsePanels");
			}
			collapse.invoke(statusBarManager);
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}

	private DownloadItem getDownloadItem(AppExtras extras) {
		DownloadItem dlItem = new DownloadItem();
		dlItem.setName(extras.getAppName());
		dlItem.setIconUrl(extras.getAppIcon());
		dlItem.setPkgName(extras.getPkgName());
		dlItem.setSource(NotificationUtil.PUSH_APP);
		dlItem.setAutoInstall(true);
		dlItem.setHidden(false);
		dlItem.setDlUrl(extras.getDlUrl());
		dlItem.setVersionCode(extras.getVersionCode());
		dlItem.setVersionName(extras.getVersionName());
		return dlItem;
	}
}
